package com.ruicar.afs.cloud.afscase.approveocr.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.afscase.approveocr.entity.ApproveOcrBatch;
import com.ruicar.afs.cloud.afscase.approveocr.entity.ApproveOcrCompare;
import com.ruicar.afs.cloud.afscase.approveocr.entity.ApproveOcrDetail;
import com.ruicar.afs.cloud.afscase.approveocr.mapper.ApproveOcrCompareMapper;
import com.ruicar.afs.cloud.afscase.approveocr.service.ApproveOcrBatchService;
import com.ruicar.afs.cloud.afscase.approveocr.service.ApproveOcrCompareService;
import com.ruicar.afs.cloud.afscase.approveocr.service.ApproveOcrDetailService;
import com.ruicar.afs.cloud.afscase.channel.common.ChannelWitnessCondition;
import com.ruicar.afs.cloud.afscase.channel.entity.*;
import com.ruicar.afs.cloud.afscase.channel.service.*;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.*;
import com.ruicar.afs.cloud.afscase.infomanagement.service.CaseBaseInfoService;
import com.ruicar.afs.cloud.afscase.infomanagement.service.CaseCarInfoService;
import com.ruicar.afs.cloud.afscase.interfacerecord.service.CaseOcrInterfaceRecordService;
import com.ruicar.afs.cloud.afscase.interfacerecord.vo.BankCardOcrDiscernVo;
import com.ruicar.afs.cloud.afscase.interfacerecord.vo.IdCardBackOcrDiscernVo;
import com.ruicar.afs.cloud.afscase.interfacerecord.vo.IdCardFrontOcrDiscernVo;
import com.ruicar.afs.cloud.afscase.infomanagement.service.CaseCustInfoService;
import com.ruicar.afs.cloud.afscase.interfacerecord.vo.InvoiceCarOcrDiscernVo;
import com.ruicar.afs.cloud.afscase.loanapprove.condition.InvoiceOcrCondition;
import com.ruicar.afs.cloud.afscase.loanapprove.entity.CarInvoiceInfo;
import com.ruicar.afs.cloud.afscase.loanapprove.entity.LoanBankCardInfo;
import com.ruicar.afs.cloud.afscase.loanapprove.service.CarInvoiceInfoService;
import com.ruicar.afs.cloud.afscase.loanapprove.service.LoanApproveService;
import com.ruicar.afs.cloud.afscase.loanapprove.service.LoanBankCardInfoService;
import com.ruicar.afs.cloud.afscase.loanapprove.vo.InvoiceCheckVo;
import com.ruicar.afs.cloud.afscase.loanapprove.vo.LoanOcrResultVO;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.*;
import com.ruicar.afs.cloud.components.datadicsync.DicHelper;
import com.ruicar.afs.cloud.components.datadicsync.dto.DicDataDto;
import com.ruicar.afs.cloud.image.condition.OcrDiscernCondition;
import com.ruicar.afs.cloud.image.entity.ComAttachmentFile;
import com.ruicar.afs.cloud.image.enums.AttachmentUniqueCodeEnum;
import com.ruicar.afs.cloud.image.service.ComAttaManageService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description ocr影像识别比较数据service
 * @Author rongji.zhang
 * @Date 2020/08/15 17:38
 */
@Slf4j
@Service
@AllArgsConstructor
public class ApproveOcrCompareServiceImpl extends ServiceImpl<ApproveOcrCompareMapper, ApproveOcrCompare> implements ApproveOcrCompareService {

    private final String inletEnd="PC";

    private final String sysCode="BOSS";

    private final int idCarDate=29;

    private final int bankLength=19;

    private LoanBankCardInfoService loanBankCardInfoService;

    private ApproveOcrBatchService approveOcrBatchService;

    private ApproveOcrDetailService approveOcrDetailService;

    private CaseOcrInterfaceRecordService caseOcrInterfaceRecordService;

    private CaseCustInfoService caseCustInfoService;

    private ComAttaManageService comAttaManageService;

    private CarInvoiceInfoService carInvoiceInfoService;

    private LoanApproveService loanApproveService;

    private CaseBaseInfoService caseBaseInfoService;

    private ChannelAffiliatedUnitsService channelAffiliatedUnitsService;

    private CaseCarInfoService caseCarInfoService;
    /**
     * @Description ocr机动车发票识别比较数据
     * @Author rongji.zhang
     * @Date 2020/08/15 17:38
     */
    @Override
    public void getInvoiceCarOcrCompare(CaseContractInfo contractInfo,ApproveOcrBatch approveOcrBatch) {
        try {
            ComAttachmentFile invoiceFile = comAttaManageService.getFile(AttachmentUniqueCodeEnum.CAR_INVOICE.getCode(), contractInfo.getContractNo());
            if(StringUtils.isBlank(invoiceFile.getFileId())){
                ApproveOcrDetail approveOcrDetail = new ApproveOcrDetail();
                approveOcrDetail.setApplyNo(contractInfo.getApplyNo());
                approveOcrDetail.setContractNo(contractInfo.getContractNo());
                approveOcrDetail.setBatchId(approveOcrBatch.getId().toString());
                approveOcrDetail.setOcrType(OcrTypeEnum.INVOICE_CAR.getValue());
                approveOcrDetail.setOcrResult(WhetherEnum.NO.getCode());
                approveOcrDetail.setRemark("未找到相应文件");
                approveOcrBatch.setOcrResult(WhetherEnum.NO.getCode());
                approveOcrDetailService.save(approveOcrDetail);
                approveOcrBatchService.updateById(approveOcrBatch);
                log.error("{}未找到相应还款机动车发票：{}",contractInfo.getContractNo());
            } else {
                List<ApproveOcrDetail> approveOcrDetailList = approveOcrDetailService.list(Wrappers.<ApproveOcrDetail>query().lambda()
                        .eq(ApproveOcrDetail::getContractNo,contractInfo.getContractNo())
                        .eq(ApproveOcrDetail::getFileId,invoiceFile.getFileId())
                        .eq(ApproveOcrDetail::getOcrType,OcrTypeEnum.INVOICE_CAR.getValue())
                        .orderByDesc(ApproveOcrDetail::getCreateTime));
                if(CollectionUtils.isEmpty(approveOcrDetailList)||StringUtils.isNotBlank(approveOcrDetailList.get(0).getRemark())){
                    ApproveOcrDetail approveOcrDetail = new ApproveOcrDetail();
                    approveOcrDetail.setApplyNo(contractInfo.getApplyNo());
                    approveOcrDetail.setContractNo(contractInfo.getContractNo());
                    approveOcrDetail.setFileId(invoiceFile.getFileId());
                    approveOcrDetail.setBatchId(approveOcrBatch.getId().toString());
                    approveOcrDetailService.save(approveOcrDetail);
                    CarInvoiceInfo carInvoiceInfo = carInvoiceInfoService.getOne(Wrappers.<CarInvoiceInfo>lambdaQuery()
                            .eq(CarInvoiceInfo::getContractNo, contractInfo.getContractNo()));
                    CaseBaseInfo caseBaseInfo = caseBaseInfoService.getOne(Wrappers.<CaseBaseInfo>query().lambda()
                            .eq(CaseBaseInfo::getApplyNo,contractInfo.getApplyNo()));
                    CaseCarInfo caseCarInfo = caseCarInfoService.getOne(Wrappers.<CaseCarInfo>query().lambda()
                            .eq(CaseCarInfo::getApplyNo,caseBaseInfo.getApplyNo()));
                    ChannelAffiliatedUnits channelAffiliatedUnits = channelAffiliatedUnitsService.getOne(Wrappers.<ChannelAffiliatedUnits>query().lambda()
                            .eq(ChannelAffiliatedUnits::getId,caseCarInfo.getAffCompanyId()));
                    OcrDiscernCondition condition = new OcrDiscernCondition();
                    condition.setCaseCode(contractInfo.getContractNo());
                    condition.setInletEnd(inletEnd);
                    condition.setSysCode(sysCode);
                    condition.setFileCode(OcrTypeEnum.INVOICE_CAR.getValue());
                    condition.setFileMd5(invoiceFile.getFileId() == null ? "" : invoiceFile.getFileId());
                    log.info("*************************************OCR发票识别开始**********************************");
                    InvoiceCarOcrDiscernVo invoice = caseOcrInterfaceRecordService.getInvoiceCarOcrDiscern(condition);
                    log.info("*************************************OCR发票识别结束**********************************");
                    if (invoice != null) {
                        int i = 0;
                        String price = invoice.getPrice().replaceAll("[^\\d.]+","");
                        //调用接口检测发票真伪
                        SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd");
                        String format ="";
                        if(invoice.getInvoiceDate()!=null){
                            format = ft.format(invoice.getInvoiceDate());
                        }
                        InvoiceOcrCondition invoiceOcrCondition = new InvoiceOcrCondition();
                        invoiceOcrCondition.setContractNo(contractInfo.getContractNo());
                        invoiceOcrCondition.setMachinePrintedCode(invoice.getInvoiceCode());
                        invoiceOcrCondition.setBillingDatas(format);
                        invoiceOcrCondition.setMachineNumber(invoice.getInvoiceNum());
                        invoiceOcrCondition.setTotalPriceTax(price);
                        InvoiceCheckVo invoiceCheckVo = loanApproveService.getInvoiceCheck(invoiceOcrCondition);
                        ApproveOcrCompare ocrCompare = new ApproveOcrCompare();
                        if(invoiceCheckVo.getCode().equals("0")){
                            ocrCompare.setDetailId(approveOcrDetail.getId().toString());
                            ocrCompare.setCompareField(OcrCompareFieldNameEnum.INVOICE_AUTHENTICITY.getValue());
                            ocrCompare.setCompareFieldName(OcrCompareFieldNameEnum.INVOICE_AUTHENTICITY.getDesc());
                            ocrCompare.setCompareNewValue(invoiceCheckVo.getMessage());
                            ocrCompare.setCompareResult(WhetherEnum.YES.getCode());
                        }else{
                            i = i+1;
                            ocrCompare.setDetailId(approveOcrDetail.getId().toString());
                            ocrCompare.setCompareField(OcrCompareFieldNameEnum.INVOICE_AUTHENTICITY.getValue());
                            ocrCompare.setCompareFieldName(OcrCompareFieldNameEnum.INVOICE_AUTHENTICITY.getDesc());
                            ocrCompare.setCompareNewValue(invoiceCheckVo.getMessage());
                            ocrCompare.setCompareResult(WhetherEnum.NO.getCode());
                        }
                        this.save(ocrCompare);
                        ApproveOcrCompare ocrCompare1 = new ApproveOcrCompare();
                        ApproveOcrCompare ocrCompare2 = new ApproveOcrCompare();
                        //无挂靠方式
                        if(caseBaseInfo.getAffiliatedWay().equals(AffiliatedWayEnum.NO.getCode())){
                            if(StringUtils.isNotBlank(caseCarInfo.getIndBusinessName()) && caseCarInfo.getIndBusinessName().equals(invoice.getPurchaser())){
                                ocrCompare1.setDetailId(approveOcrDetail.getId().toString());
                                ocrCompare1.setCompareField(OcrCompareFieldNameEnum.INVOICE_CUST_NAME.getValue());
                                ocrCompare1.setCompareFieldName(OcrCompareFieldNameEnum.INVOICE_CUST_NAME.getDesc());
                                ocrCompare1.setCompareNewValue(invoice.getPurchaser());
                                ocrCompare1.setCompareResult(WhetherEnum.YES.getCode());
                            } else {
                                i = i+1;
                                ocrCompare1.setDetailId(approveOcrDetail.getId().toString());
                                ocrCompare1.setCompareField(OcrCompareFieldNameEnum.INVOICE_CUST_NAME.getValue());
                                ocrCompare1.setCompareFieldName(OcrCompareFieldNameEnum.INVOICE_CUST_NAME.getDesc());
                                ocrCompare1.setCompareNewValue(invoice.getPurchaser());
                                ocrCompare1.setCompareResult(WhetherEnum.NO.getCode());
                            }
                            this.save(ocrCompare1);
                            if(StringUtils.isNotBlank(caseBaseInfo.getCertNoRepeat()) && caseBaseInfo.getCertNoRepeat().equals(invoice.getPurchaserCode())){
                                ocrCompare2.setDetailId(approveOcrDetail.getId().toString());
                                ocrCompare2.setCompareField(OcrCompareFieldNameEnum.INVOICE_CUST_NO.getValue());
                                ocrCompare2.setCompareFieldName(OcrCompareFieldNameEnum.INVOICE_CUST_NO.getDesc());
                                ocrCompare2.setCompareNewValue(invoice.getPurchaserCode());
                                ocrCompare2.setCompareResult(WhetherEnum.YES.getCode());
                            } else {
                                i = i+1;
                                ocrCompare2.setDetailId(approveOcrDetail.getId().toString());
                                ocrCompare2.setCompareField(OcrCompareFieldNameEnum.INVOICE_CUST_NO.getValue());
                                ocrCompare2.setCompareFieldName(OcrCompareFieldNameEnum.INVOICE_CUST_NO.getDesc());
                                ocrCompare2.setCompareNewValue(invoice.getPurchaserCode());
                                ocrCompare2.setCompareResult(WhetherEnum.NO.getCode());
                            }
                            this.save(ocrCompare2);
                        }
                        //个体工商户挂靠
                        if(caseBaseInfo.getAffiliatedWay().equals(AffiliatedWayEnum.PERSONAL_AFFILIATED.getCode())){
                            if(StringUtils.isNotBlank(caseBaseInfo.getCustNameRepeat()) && caseBaseInfo.getCustNameRepeat().equals(invoice.getPurchaser())){
                                ocrCompare1.setDetailId(approveOcrDetail.getId().toString());
                                ocrCompare1.setCompareField(OcrCompareFieldNameEnum.INVOICE_CUST_NAME.getValue());
                                ocrCompare1.setCompareFieldName(OcrCompareFieldNameEnum.INVOICE_CUST_NAME.getDesc());
                                ocrCompare1.setCompareNewValue(invoice.getPurchaser());
                                ocrCompare1.setCompareResult(WhetherEnum.YES.getCode());
                            } else {
                                i = i+1;
                                ocrCompare1.setDetailId(approveOcrDetail.getId().toString());
                                ocrCompare1.setCompareField(OcrCompareFieldNameEnum.INVOICE_CUST_NAME.getValue());
                                ocrCompare1.setCompareFieldName(OcrCompareFieldNameEnum.INVOICE_CUST_NAME.getDesc());
                                ocrCompare1.setCompareNewValue(invoice.getPurchaser());
                                ocrCompare1.setCompareResult(WhetherEnum.NO.getCode());
                            }
                            this.save(ocrCompare1);
                            if(StringUtils.isNotBlank(caseCarInfo.getIndBusinessUsci()) && caseCarInfo.getIndBusinessUsci().equals(invoice.getPurchaserCode())){
                                ocrCompare2.setDetailId(approveOcrDetail.getId().toString());
                                ocrCompare2.setCompareField(OcrCompareFieldNameEnum.SOC_UNI_CRT_CDOE.getValue());
                                ocrCompare2.setCompareFieldName(OcrCompareFieldNameEnum.SOC_UNI_CRT_CDOE.getDesc());
                                ocrCompare2.setCompareNewValue(invoice.getPurchaserCode());
                                ocrCompare2.setCompareResult(WhetherEnum.YES.getCode());
                            } else {
                                i = i+1;
                                ocrCompare2.setDetailId(approveOcrDetail.getId().toString());
                                ocrCompare2.setCompareField(OcrCompareFieldNameEnum.SOC_UNI_CRT_CDOE.getValue());
                                ocrCompare2.setCompareFieldName(OcrCompareFieldNameEnum.SOC_UNI_CRT_CDOE.getDesc());
                                ocrCompare2.setCompareNewValue(invoice.getPurchaserCode());
                                ocrCompare2.setCompareResult(WhetherEnum.NO.getCode());
                            }
                            this.save(ocrCompare2);
                        }
                        //第三方公司挂靠&&网约车挂靠
                        if(caseBaseInfo.getAffiliatedWay().equals(AffiliatedWayEnum.COMPANY_AFFILIATED.getCode())
                        || caseBaseInfo.getAffiliatedWay().equals(AffiliatedWayEnum.NETWORK_CAR_AFFILIATED.getCode())){
                            if(StringUtils.isNotBlank(channelAffiliatedUnits.getAffiliatedName()) && channelAffiliatedUnits.getAffiliatedName().equals(invoice.getPurchaser())){
                                ocrCompare1.setDetailId(approveOcrDetail.getId().toString());
                                ocrCompare1.setCompareField(OcrCompareFieldNameEnum.INVOICE_CUST_NAME.getValue());
                                ocrCompare1.setCompareFieldName(OcrCompareFieldNameEnum.INVOICE_CUST_NAME.getDesc());
                                ocrCompare1.setCompareNewValue(invoice.getPurchaser());
                                ocrCompare1.setCompareResult(WhetherEnum.YES.getCode());
                            } else {
                                i = i+1;
                                ocrCompare1.setDetailId(approveOcrDetail.getId().toString());
                                ocrCompare1.setCompareField(OcrCompareFieldNameEnum.INVOICE_CUST_NAME.getValue());
                                ocrCompare1.setCompareFieldName(OcrCompareFieldNameEnum.INVOICE_CUST_NAME.getDesc());
                                ocrCompare1.setCompareNewValue(invoice.getPurchaser());
                                ocrCompare1.setCompareResult(WhetherEnum.NO.getCode());
                            }
                            this.save(ocrCompare1);
                            if(StringUtils.isNotBlank(caseCarInfo.getIndBusinessUsci()) && caseCarInfo.getIndBusinessUsci().equals(invoice.getPurchaserCode())){
                                ocrCompare2.setDetailId(approveOcrDetail.getId().toString());
                                ocrCompare2.setCompareField(OcrCompareFieldNameEnum.SOC_UNI_CRT_CDOE.getValue());
                                ocrCompare2.setCompareFieldName(OcrCompareFieldNameEnum.SOC_UNI_CRT_CDOE.getDesc());
                                ocrCompare2.setCompareNewValue(invoice.getPurchaserCode());
                                ocrCompare2.setCompareResult(WhetherEnum.YES.getCode());
                            } else {
                                i = i+1;
                                ocrCompare2.setDetailId(approveOcrDetail.getId().toString());
                                ocrCompare2.setCompareField(OcrCompareFieldNameEnum.SOC_UNI_CRT_CDOE.getValue());
                                ocrCompare2.setCompareFieldName(OcrCompareFieldNameEnum.SOC_UNI_CRT_CDOE.getDesc());
                                ocrCompare2.setCompareNewValue(invoice.getPurchaserCode());
                                ocrCompare2.setCompareResult(WhetherEnum.NO.getCode());
                            }
                            this.save(ocrCompare2);
                        }
                        Map<String, Object> oldInvoice = new LinkedHashMap<>();
                        Map<String, Object> newInvoice = new LinkedHashMap<String, Object>();
                        if(StringUtils.isNotBlank(carInvoiceInfo.getInvoiceCode())){
                            String paramInvoiceDate = ft.format(carInvoiceInfo.getInvoiceDate());
                            oldInvoice.put(OcrCompareFieldNameEnum.INVOICE_DATE.getValue(),paramInvoiceDate);
                        } else {
                            oldInvoice.put(OcrCompareFieldNameEnum.INVOICE_DATE.getValue(),"");
                        }
                        oldInvoice.put(OcrCompareFieldNameEnum.INVOICE_AMT.getValue(),carInvoiceInfo.getInvoiceAmt());
                        oldInvoice.put(OcrCompareFieldNameEnum.CAR_VIN.getValue(),carInvoiceInfo.getCarVin());
                        oldInvoice.put(OcrCompareFieldNameEnum.MANU_MODEL.getValue(), carInvoiceInfo.getFullModelNum());
                        oldInvoice.put(OcrCompareFieldNameEnum.ENGINE_NUM.getValue(), carInvoiceInfo.getEngineNo());
                        newInvoice.put(OcrCompareFieldNameEnum.INVOICE_DATE.getValue(),format);
                        newInvoice.put(OcrCompareFieldNameEnum.INVOICE_AMT.getValue(),price);
                        newInvoice.put(OcrCompareFieldNameEnum.CAR_VIN.getValue(),invoice.getVinNum());
                        newInvoice.put(OcrCompareFieldNameEnum.MANU_MODEL.getValue(), invoice.getManuModel());
                        newInvoice.put(OcrCompareFieldNameEnum.ENGINE_NUM.getValue(), invoice.getEngineNum());
                        Iterator<Map.Entry<String, Object>> iter1 = oldInvoice.entrySet().iterator();
                        while (iter1.hasNext()) {
                            ApproveOcrCompare approveOcrCompare = new ApproveOcrCompare();
                            Map.Entry<String, Object> entry1 = iter1.next();
                            Object m1value = entry1.getValue() == null ? "" : entry1.getValue();
                            Object m2value = newInvoice.get(entry1.getKey()) == null ? "" : newInvoice.get(entry1.getKey());
                            String m1key = entry1.getKey() == null ? "" : entry1.getKey();
                            if (!m1value.equals(m2value)) {//若两个map中相同key对应的value不相等
                                approveOcrCompare.setCompareResult(WhetherEnum.NO.getCode());
                                i = i + 1;
                            } else {
                                approveOcrCompare.setCompareResult(WhetherEnum.YES.getCode());
                            }
                            approveOcrCompare.setDetailId(approveOcrDetail.getId().toString());
                            approveOcrCompare.setCompareField(m1key);
                            if(m1key.equals(OcrCompareFieldNameEnum.MANU_MODEL.getValue())){
                                approveOcrCompare.setCompareFieldName(OcrCompareFieldNameEnum.MANU_MODEL.getDesc());
                            }
                            if(m1key.equals(OcrCompareFieldNameEnum.ENGINE_NUM.getValue())){
                                approveOcrCompare.setCompareFieldName(OcrCompareFieldNameEnum.ENGINE_NUM.getDesc());
                            }
                            if(m1key.equals(OcrCompareFieldNameEnum.INVOICE_AMT.getValue())){
                                approveOcrCompare.setCompareFieldName(OcrCompareFieldNameEnum.INVOICE_AMT.getDesc());
                            }
                            if(m1key.equals(OcrCompareFieldNameEnum.INVOICE_CUST_NAME.getValue())){
                                approveOcrCompare.setCompareFieldName(OcrCompareFieldNameEnum.INVOICE_CUST_NAME.getDesc());
                            }
                            if(m1key.equals(OcrCompareFieldNameEnum.INVOICE_CUST_NO.getValue())){
                                approveOcrCompare.setCompareFieldName(OcrCompareFieldNameEnum.INVOICE_CUST_NO.getDesc());
                            }
                            if(m1key.equals(OcrCompareFieldNameEnum.CAR_VIN.getValue())){
                                approveOcrCompare.setCompareFieldName(OcrCompareFieldNameEnum.CAR_VIN.getDesc());
                            }
                            if(m1key.equals(OcrCompareFieldNameEnum.INVOICE_DATE.getValue())){
                                approveOcrCompare.setCompareFieldName(OcrCompareFieldNameEnum.INVOICE_DATE.getDesc());
                            }
                            approveOcrCompare.setCompareNewValue(m2value.toString());
                            this.save(approveOcrCompare);
                        }
                        if (i > 0) {
                            approveOcrDetail.setOcrType(OcrTypeEnum.INVOICE_CAR.getValue());
                            approveOcrDetail.setOcrResult(WhetherEnum.NO.getCode());
                            approveOcrBatch.setOcrResult(WhetherEnum.NO.getCode());
                        } else {
                            approveOcrDetail.setOcrType(OcrTypeEnum.INVOICE_CAR.getValue());
                            approveOcrDetail.setOcrResult(WhetherEnum.YES.getCode());
                        }
                        approveOcrDetailService.updateById(approveOcrDetail);
                        approveOcrBatchService.updateById(approveOcrBatch);
                    } else {
                        approveOcrDetail.setOcrType(OcrTypeEnum.INVOICE_CAR.getValue());
                        approveOcrDetail.setOcrResult(WhetherEnum.NO.getCode());
                        approveOcrDetail.setRemark("OCR识别失败");
                        approveOcrBatch.setOcrResult(WhetherEnum.NO.getCode());
                        approveOcrDetailService.updateById(approveOcrDetail);
                        approveOcrBatchService.updateById(approveOcrBatch);
                    }
                }
            }
        }catch (Exception e){
            ApproveOcrDetail approveOcrDetail = new ApproveOcrDetail();
            approveOcrDetail.setApplyNo(contractInfo.getApplyNo());
            approveOcrDetail.setContractNo(contractInfo.getContractNo());
            approveOcrDetail.setBatchId(approveOcrBatch.getId().toString());
            approveOcrDetail.setOcrType(OcrTypeEnum.INVOICE_CAR.getValue());
            approveOcrDetail.setOcrResult(WhetherEnum.NO.getCode());
            approveOcrDetail.setRemark("识别失败");
            approveOcrBatch.setOcrResult(WhetherEnum.NO.getCode());
            approveOcrDetailService.save(approveOcrDetail);
            approveOcrBatchService.updateById(approveOcrBatch);
            log.error("{}还款机动车发票OCR识别失败：{}",contractInfo.getContractNo(),e);
        }
    }

    /**
     * @Description ocr银行卡识别比较数据
     * @Author rongji.zhang
     * @Date 2020/08/15 17:38
     */
    @Override
    public void getBankOcrCompare(CaseContractInfo contractInfo,ApproveOcrBatch approveOcrBatch) {
        try {
            ComAttachmentFile bankFile = comAttaManageService.getFile(AttachmentUniqueCodeEnum.REPAY_BANK_CARD_FRONT.getCode(), contractInfo.getContractNo());
            if(StringUtils.isBlank(bankFile.getFileId())){
                ApproveOcrDetail approveOcrDetail = new ApproveOcrDetail();
                approveOcrDetail.setApplyNo(contractInfo.getApplyNo());
                approveOcrDetail.setContractNo(contractInfo.getContractNo());
                approveOcrDetail.setBatchId(approveOcrBatch.getId().toString());
                approveOcrDetail.setOcrType(OcrTypeEnum.BANK_CARD.getValue());
                approveOcrDetail.setOcrResult(WhetherEnum.NO.getCode());
                approveOcrDetail.setRemark("未找到相应文件");
                approveOcrBatch.setOcrResult(WhetherEnum.NO.getCode());
                approveOcrDetailService.save(approveOcrDetail);
                approveOcrBatchService.updateById(approveOcrBatch);
                log.error("{}未找到还款人银行卡文件：{}",contractInfo.getContractNo());
            } else {
                List<ApproveOcrDetail> approveOcrDetailList = approveOcrDetailService.list(Wrappers.<ApproveOcrDetail>query().lambda()
                        .eq(ApproveOcrDetail::getContractNo,contractInfo.getContractNo())
                        .eq(ApproveOcrDetail::getFileId,bankFile.getFileId())
                        .eq(ApproveOcrDetail::getOcrType,OcrTypeEnum.BANK_CARD.getValue())
                        .orderByDesc(ApproveOcrDetail::getCreateTime));
                if(CollectionUtils.isEmpty(approveOcrDetailList)||StringUtils.isNotBlank(approveOcrDetailList.get(0).getRemark())){
                    ApproveOcrDetail approveOcrDetail = new ApproveOcrDetail();
                    approveOcrDetail.setApplyNo(contractInfo.getApplyNo());
                    approveOcrDetail.setContractNo(contractInfo.getContractNo());
                    approveOcrDetail.setFileId(bankFile.getFileId());
                    approveOcrDetail.setBatchId(approveOcrBatch.getId().toString());
                    approveOcrDetailService.save(approveOcrDetail);
                    LoanBankCardInfo loanBankCardInfo = loanBankCardInfoService.getOne(Wrappers.<LoanBankCardInfo>lambdaQuery()
                            .eq(LoanBankCardInfo::getContractNo, contractInfo.getContractNo()));
                    String bankName ="";
                    Map<String, List<DicDataDto>> map = DicHelper.getDicMaps("bankCode");
                    List<DicDataDto> list=map.get("bankCode");
                    for (DicDataDto dto : list){
                        if(dto.getValue().equals(loanBankCardInfo.getBankCode())){
                            bankName = dto.getTitle();
                        }
                    }
                    OcrDiscernCondition condition = new OcrDiscernCondition();
                    condition.setCaseCode(contractInfo.getContractNo());
                    condition.setInletEnd(inletEnd);
                    condition.setSysCode(sysCode);
                    condition.setFileCode(OcrTypeEnum.BANK_CARD.getValue());
                    condition.setFileMd5(bankFile.getFileId() == null ? "" : bankFile.getFileId());
                    log.info("*************************************OCR银行卡识别开始**********************************");
                    BankCardOcrDiscernVo bank = caseOcrInterfaceRecordService.getBankCardOcrDiscern(condition);
                    log.info("*************************************OCR银行卡识别结束**********************************");
                    if (bank != null) {
                        int i = 0;
                        ApproveOcrCompare ocrCompare = new ApproveOcrCompare();
                        if(StringUtils.isBlank(bankName) || StringUtils.isBlank(bank.getBankName())){
                            i = i+1;
                            ocrCompare.setDetailId(approveOcrDetail.getId().toString());
                            ocrCompare.setCompareField(OcrCompareFieldNameEnum.BANK_NAME.getValue());
                            ocrCompare.setCompareFieldName(OcrCompareFieldNameEnum.BANK_NAME.getDesc());
                            ocrCompare.setCompareNewValue(bank.getBankName());
                            ocrCompare.setCompareResult(WhetherEnum.NO.getCode());
                        } else {
                            if(bankName.indexOf(bank.getBankName())==-1 &&
                                    bank.getBankName().indexOf(bankName)==-1){
                                i = i+1;
                                ocrCompare.setDetailId(approveOcrDetail.getId().toString());
                                ocrCompare.setCompareField(OcrCompareFieldNameEnum.BANK_NAME.getValue());
                                ocrCompare.setCompareFieldName(OcrCompareFieldNameEnum.BANK_NAME.getDesc());
                                ocrCompare.setCompareNewValue(bank.getBankName());
                                ocrCompare.setCompareResult(WhetherEnum.NO.getCode());
                            }else {
                                ocrCompare.setDetailId(approveOcrDetail.getId().toString());
                                ocrCompare.setCompareField(OcrCompareFieldNameEnum.BANK_NAME.getValue());
                                ocrCompare.setCompareFieldName(OcrCompareFieldNameEnum.BANK_NAME.getDesc());
                                ocrCompare.setCompareNewValue(bank.getBankName());
                                ocrCompare.setCompareResult(WhetherEnum.YES.getCode());
                            }
                        }
                        this.save(ocrCompare);
                        Map<String, Object> oldBank = new LinkedHashMap<String, Object>();
                        Map<String, Object> newBank = new LinkedHashMap<String, Object>();
                        oldBank.put(OcrCompareFieldNameEnum.ACCOUNT_NO.getValue(), loanBankCardInfo.getAccountNo());
                        newBank.put(OcrCompareFieldNameEnum.ACCOUNT_NO.getValue(), bank.getBankCardNumber().replaceAll(" ",""));
                        Iterator<Map.Entry<String, Object>> iter1 = oldBank.entrySet().iterator();
                        while (iter1.hasNext()) {
                            ApproveOcrCompare approveOcrCompare = new ApproveOcrCompare();
                            Map.Entry<String, Object> entry1 = iter1.next();
                            Object m1value = entry1.getValue() == null ? "" : entry1.getValue();
                            Object m2value = newBank.get(entry1.getKey()) == null ? "" : newBank.get(entry1.getKey());
                            String m1key = entry1.getKey() == null ? "" : entry1.getKey();
                            if (!m1value.equals(m2value)) {//若两个map中相同key对应的value不相等
                                approveOcrCompare.setCompareResult(WhetherEnum.NO.getCode());
                                i = i + 1;
                            } else {
                                approveOcrCompare.setCompareResult(WhetherEnum.YES.getCode());
                            }
                            approveOcrCompare.setDetailId(approveOcrDetail.getId().toString());
                            approveOcrCompare.setCompareField(m1key);
                            if(m1key.equals(OcrCompareFieldNameEnum.ACCOUNT_NO.getValue())){
                                approveOcrCompare.setCompareFieldName(OcrCompareFieldNameEnum.ACCOUNT_NO.getDesc());
                            }
                            if(m1key.equals(OcrCompareFieldNameEnum.BANK_NAME.getValue())){
                                approveOcrCompare.setCompareFieldName(OcrCompareFieldNameEnum.BANK_NAME.getDesc());
                            }
                            approveOcrCompare.setCompareNewValue(m2value.toString());
                            this.save(approveOcrCompare);
                        }
                        ApproveOcrCompare ocrCompare1 = new ApproveOcrCompare();
                        if(bank.getBankCardNumber().replaceAll(" ","").length()!=bankLength){
                            i = i+1;
                            ocrCompare1.setDetailId(approveOcrDetail.getId().toString());
                            ocrCompare1.setCompareField(OcrCompareFieldNameEnum.BANK_LENGTH.getValue());
                            ocrCompare1.setCompareFieldName(OcrCompareFieldNameEnum.BANK_LENGTH.getDesc());
                            ocrCompare1.setCompareResult(WhetherEnum.NO.getCode());
                        }else {
                            ocrCompare1.setDetailId(approveOcrDetail.getId().toString());
                            ocrCompare1.setCompareField(OcrCompareFieldNameEnum.BANK_LENGTH.getValue());
                            ocrCompare1.setCompareFieldName(OcrCompareFieldNameEnum.BANK_LENGTH.getDesc());
                            ocrCompare1.setCompareResult(WhetherEnum.YES.getCode());
                        }
                        this.save(ocrCompare1);
                        if (i > 0) {
                            approveOcrDetail.setOcrType(OcrTypeEnum.BANK_CARD.getValue());
                            approveOcrDetail.setOcrResult(WhetherEnum.NO.getCode());
                            approveOcrBatch.setOcrResult(WhetherEnum.NO.getCode());
                        } else {
                            approveOcrDetail.setOcrType(OcrTypeEnum.BANK_CARD.getValue());
                            approveOcrDetail.setOcrResult(WhetherEnum.YES.getCode());
                        }
                        approveOcrDetailService.updateById(approveOcrDetail);
                        approveOcrBatchService.updateById(approveOcrBatch);
                    } else {
                        approveOcrDetail.setOcrType(OcrTypeEnum.BANK_CARD.getValue());
                        approveOcrDetail.setOcrResult(WhetherEnum.NO.getCode());
                        approveOcrDetail.setRemark("OCR识别失败");
                        approveOcrBatch.setOcrResult(WhetherEnum.NO.getCode());
                        approveOcrDetailService.updateById(approveOcrDetail);
                        approveOcrBatchService.updateById(approveOcrBatch);
                    }
                }
            }
        }catch (Exception e){
            ApproveOcrDetail approveOcrDetail = new ApproveOcrDetail();
            approveOcrDetail.setApplyNo(contractInfo.getApplyNo());
            approveOcrDetail.setContractNo(contractInfo.getContractNo());
            approveOcrDetail.setBatchId(approveOcrBatch.getId().toString());
            approveOcrDetail.setOcrType(OcrTypeEnum.BANK_CARD.getValue());
            approveOcrDetail.setOcrResult(WhetherEnum.NO.getCode());
            approveOcrDetail.setRemark("识别失败");
            approveOcrBatch.setOcrResult(WhetherEnum.NO.getCode());
            approveOcrDetailService.save(approveOcrDetail);
            approveOcrBatchService.updateById(approveOcrBatch);
            log.error("{}还款银行卡正面OCR识别失败：{}",contractInfo.getContractNo(),e);
        }
    }

    /**
     * @Description ocr身份证正面识别比较数据
     * @Author rongji.zhang
     * @Date 2020/08/15 17:38
     */
    @Override
    public void getIdFrontOcrCompare(CaseContractInfo contractInfo, ApproveOcrBatch approveOcrBatch) {
        try {
            ComAttachmentFile frontFile = comAttaManageService.getFile(AttachmentUniqueCodeEnum.REPAY_IDCARD_FRONT.getCode(), contractInfo.getContractNo());
            ComAttachmentFile backFile = comAttaManageService.getFile(AttachmentUniqueCodeEnum.REPAY_IDCARD_BACK.getCode(), contractInfo.getContractNo());
            if(StringUtils.isBlank(frontFile.getFileId()) || StringUtils.isBlank(backFile.getFileId())){
                ApproveOcrDetail approveOcrDetail = new ApproveOcrDetail();
                approveOcrDetail.setApplyNo(contractInfo.getApplyNo());
                approveOcrDetail.setContractNo(contractInfo.getContractNo());
                approveOcrDetail.setBatchId(approveOcrBatch.getId().toString());
                approveOcrDetail.setOcrType(OcrTypeEnum.ID_CARD.getValue());
                approveOcrDetail.setOcrResult(WhetherEnum.NO.getCode());
                approveOcrDetail.setRemark("未找到相应文件");
                approveOcrBatch.setOcrResult(WhetherEnum.NO.getCode());
                approveOcrDetailService.save(approveOcrDetail);
                approveOcrBatchService.updateById(approveOcrBatch);
                log.error("{}未找到还款人身份证文件：{}",contractInfo.getContractNo());
            } else {
                List<ApproveOcrDetail> approveOcrDetailList = approveOcrDetailService.list(Wrappers.<ApproveOcrDetail>query().lambda()
                        .eq(ApproveOcrDetail::getContractNo,contractInfo.getContractNo())
                        .eq(ApproveOcrDetail::getFileId,frontFile.getFileId())
                        .eq(ApproveOcrDetail::getOcrType,OcrTypeEnum.ID_CARD.getValue())
                        .orderByDesc(ApproveOcrDetail::getCreateTime));
                if(CollectionUtils.isEmpty(approveOcrDetailList)||StringUtils.isNotBlank(approveOcrDetailList.get(0).getRemark())){
                    ApproveOcrDetail approveOcrDetail = new ApproveOcrDetail();
                    approveOcrDetail.setApplyNo(contractInfo.getApplyNo());
                    approveOcrDetail.setContractNo(contractInfo.getContractNo());
                    approveOcrDetail.setBatchId(approveOcrBatch.getId().toString());
                    approveOcrDetail.setFileId(frontFile.getFileId());
                    approveOcrDetailService.save(approveOcrDetail);
                    LoanBankCardInfo loanBankCardInfo = loanBankCardInfoService.getOne(Wrappers.<LoanBankCardInfo>lambdaQuery()
                            .eq(LoanBankCardInfo::getApplyNo, contractInfo.getApplyNo())
                            .eq(LoanBankCardInfo::getContractNo,contractInfo.getContractNo()));
                    CaseCustInfo caseCustInfo = caseCustInfoService.getOne(Wrappers.<CaseCustInfo>query().lambda()
                            .eq(CaseCustInfo::getApplyNo,contractInfo.getApplyNo())
                            .eq(StringUtils.isNotBlank(loanBankCardInfo.getCertNo()),CaseCustInfo::getCertNo,loanBankCardInfo.getCertNo()),false);
                    OcrDiscernCondition condition = new OcrDiscernCondition();
                    condition.setCaseCode(contractInfo.getContractNo());
                    condition.setInletEnd(inletEnd);
                    condition.setSysCode(sysCode);
                    condition.setFileCode(OcrTypeEnum.ID_CARD_FRONT.getValue());
                    condition.setFileMd5(frontFile.getFileId() == null ? "" : frontFile.getFileId());
                    log.info("*************************************OCR身份证正面识别开始**********************************");
                    IdCardFrontOcrDiscernVo idCardFront = caseOcrInterfaceRecordService.getIdCardFrontOcrDiscern(condition);
                    log.info("*************************************OCR身份证正面识别结束**********************************");
                    OcrDiscernCondition condition1 = new OcrDiscernCondition();
                    condition1.setCaseCode(contractInfo.getContractNo());
                    condition1.setInletEnd(inletEnd);
                    condition1.setSysCode(sysCode);
                    condition1.setFileCode(OcrTypeEnum.ID_CARD_BACK.getValue());
                    condition1.setFileMd5(backFile.getFileId() == null ? "" : backFile.getFileId());
                    log.info("*************************************OCR身份证反面识别开始**********************************");
                    IdCardBackOcrDiscernVo idCardBack = caseOcrInterfaceRecordService.getIdCardBackOcrDiscern(condition1);
                    log.info("*************************************OCR身份证反面识别结束**********************************");
                    if (idCardFront != null && idCardBack != null) {
                        int i = 0;
                        Map<String, Object> oldFront = new LinkedHashMap<String, Object>();
                        Map<String, Object> newFront = new LinkedHashMap<String, Object>();
                        oldFront.put(OcrCompareFieldNameEnum.CUST_NAME.getValue(), caseCustInfo.getCustName());
                        oldFront.put(OcrCompareFieldNameEnum.CERT_NO.getValue(), caseCustInfo.getCertNo());
                        newFront.put(OcrCompareFieldNameEnum.CUST_NAME.getValue(), idCardFront.getName());
                        newFront.put(OcrCompareFieldNameEnum.CERT_NO.getValue(), idCardFront.getNumber());
                        Iterator<Map.Entry<String, Object>> iter1 = oldFront.entrySet().iterator();
                        while (iter1.hasNext()) {
                            ApproveOcrCompare approveOcrCompare = new ApproveOcrCompare();
                            Map.Entry<String, Object> entry1 = iter1.next();
                            Object m1value = entry1.getValue() == null ? "" : entry1.getValue();
                            Object m2value = newFront.get(entry1.getKey()) == null ? "" : newFront.get(entry1.getKey());
                            String m1key = entry1.getKey() == null ? "" : entry1.getKey();
                            if (!m1value.equals(m2value)) {//若两个map中相同key对应的value不相等
                                approveOcrCompare.setCompareResult(WhetherEnum.NO.getCode());
                                i = i + 1;
                            } else {
                                approveOcrCompare.setCompareResult(WhetherEnum.YES.getCode());
                            }
                            approveOcrCompare.setDetailId(approveOcrDetail.getId().toString());
                            if(m1key.equals(OcrCompareFieldNameEnum.CUST_NAME.getValue())){
                                approveOcrCompare.setCompareFieldName(OcrCompareFieldNameEnum.CUST_NAME.getDesc());
                            }
                            if(m1key.equals(OcrCompareFieldNameEnum.CERT_NO.getValue())){
                                approveOcrCompare.setCompareFieldName(OcrCompareFieldNameEnum.CERT_NO.getDesc());
                            }
                            approveOcrCompare.setCompareField(m1key);
                            approveOcrCompare.setCompareNewValue(m2value.toString());
                            this.save(approveOcrCompare);
                        }
                        Map<String, Object> oldBack = new LinkedHashMap<String, Object>();
                        Map<String, Object> newBack = new LinkedHashMap<String, Object>();
                        oldBack.put(OcrCompareFieldNameEnum.CERT_END_DATE.getValue(), caseCustInfo.getCertEndDate());
                        newBack.put(OcrCompareFieldNameEnum.CERT_END_DATE.getValue(), idCardBack.getLssueDate());
                        Iterator<Map.Entry<String, Object>> iter2 = oldBack.entrySet().iterator();
                        while (iter2.hasNext()) {
                            ApproveOcrCompare approveOcrCompare = new ApproveOcrCompare();
                            Map.Entry<String, Object> entry2 = iter2.next();
                            Object m1value = entry2.getValue() == null ? "" : entry2.getValue();
                            Object m2value = newBack.get(entry2.getKey()) == null ? "" : newBack.get(entry2.getKey());
                            String m1key = entry2.getKey() == null ? "" : entry2.getKey();
                            if (!m1value.equals(m2value)) {//若两个map中相同key对应的value不相等
                                approveOcrCompare.setCompareResult(WhetherEnum.NO.getCode());
                                i = i + 1;
                            } else {
                                approveOcrCompare.setCompareResult(WhetherEnum.YES.getCode());
                            }
                            approveOcrCompare.setDetailId(approveOcrDetail.getId().toString());
                            if(m1key.equals(OcrCompareFieldNameEnum.CERT_END_DATE.getValue())){
                                approveOcrCompare.setCompareFieldName(OcrCompareFieldNameEnum.CERT_END_DATE.getDesc());
                            }
                            approveOcrCompare.setCompareField(m1key);
                            approveOcrCompare.setCompareNewValue(m2value.toString());
                            this.save(approveOcrCompare);
                        }
                        ApproveOcrCompare ocrCompare = new ApproveOcrCompare();
                        if(idCardBack.getLssueDate()!=null){
                            if(((new Date().getTime()-idCardBack.getLssueDate().getTime()+1000000)/(60*60*24*1000))>idCarDate){
                                ocrCompare.setDetailId(approveOcrDetail.getId().toString());
                                ocrCompare.setCompareField(OcrCompareFieldNameEnum.LATER_END_DATE.getValue());
                                ocrCompare.setCompareFieldName(OcrCompareFieldNameEnum.LATER_END_DATE.getDesc());
                                ocrCompare.setCompareResult(WhetherEnum.YES.getCode());
                            }else {
                                i = i+1;
                                ocrCompare.setDetailId(approveOcrDetail.getId().toString());
                                ocrCompare.setCompareField(OcrCompareFieldNameEnum.LATER_END_DATE.getValue());
                                ocrCompare.setCompareFieldName(OcrCompareFieldNameEnum.LATER_END_DATE.getDesc());
                                ocrCompare.setCompareResult(WhetherEnum.NO.getCode());
                            }
                        } else {
                            i = i+1;
                            ocrCompare.setDetailId(approveOcrDetail.getId().toString());
                            ocrCompare.setCompareNewValue("OCR未验证出身份证有效期。");
                            ocrCompare.setCompareField(OcrCompareFieldNameEnum.LATER_END_DATE.getValue());
                            ocrCompare.setCompareFieldName(OcrCompareFieldNameEnum.LATER_END_DATE.getDesc());
                            ocrCompare.setCompareResult(WhetherEnum.NO.getCode());
                        }
                        this.save(ocrCompare);
                        if (i > 0) {
                            approveOcrDetail.setOcrType(OcrTypeEnum.ID_CARD.getValue());
                            approveOcrDetail.setOcrResult(WhetherEnum.NO.getCode());
                            approveOcrBatch.setOcrResult(WhetherEnum.NO.getCode());
                        } else {
                            approveOcrDetail.setOcrType(OcrTypeEnum.ID_CARD.getValue());
                            approveOcrDetail.setOcrResult(WhetherEnum.YES.getCode());
                        }
                        approveOcrDetailService.updateById(approveOcrDetail);
                        approveOcrBatchService.updateById(approveOcrBatch);
                    } else {
                        approveOcrDetail.setOcrType(OcrTypeEnum.ID_CARD.getValue());
                        approveOcrDetail.setOcrResult(WhetherEnum.NO.getCode());
                        approveOcrDetail.setRemark("OCR识别失败");
                        approveOcrBatch.setOcrResult(WhetherEnum.NO.getCode());
                        approveOcrDetailService.updateById(approveOcrDetail);
                        approveOcrBatchService.updateById(approveOcrBatch);
                    }
                }
            }
        }catch (Exception e){
            ApproveOcrDetail approveOcrDetail = new ApproveOcrDetail();
            approveOcrDetail.setApplyNo(contractInfo.getApplyNo());
            approveOcrDetail.setContractNo(contractInfo.getContractNo());
            approveOcrDetail.setBatchId(approveOcrBatch.getId().toString());
            approveOcrDetail.setOcrType(OcrTypeEnum.ID_CARD.getValue());
            approveOcrDetail.setOcrResult(WhetherEnum.NO.getCode());
            approveOcrDetail.setRemark("识别失败");
            approveOcrBatch.setOcrResult(WhetherEnum.NO.getCode());
            approveOcrDetailService.save(approveOcrDetail);
            approveOcrBatchService.updateById(approveOcrBatch);
            log.error("{}还款人身份证OCR识别失败：{}",contractInfo.getContractNo(),e);
        }
    }
    /**
     * 获取OCR验证结果
     * @param condition
     * @return
     */
    @Override
    public List<LoanOcrResultVO> getOcrResult(ChannelWitnessCondition condition) {
        //获取最近发票验证结果
        List<LoanOcrResultVO> loanOcrResultVOList = new ArrayList<>();
        List<ApproveOcrDetail> invoiceOcrDetailList = approveOcrDetailService.list(Wrappers.<ApproveOcrDetail>query().lambda()
                .eq(ApproveOcrDetail::getContractNo,condition.getContractNo())
                .eq(ApproveOcrDetail::getOcrType,OcrTypeEnum.INVOICE_CAR.getValue())
                .orderByDesc(ApproveOcrDetail::getCreateTime));
        if(CollectionUtils.isNotEmpty(invoiceOcrDetailList)){
            if(invoiceOcrDetailList.get(0).getOcrResult()!=null && invoiceOcrDetailList.get(0).getOcrResult()!=""){
                LoanOcrResultVO loanOcrResultVO = new LoanOcrResultVO();
                List<ApproveOcrCompare> approveOcrCompareList = this.list(Wrappers.<ApproveOcrCompare>query().lambda()
                        .eq(ApproveOcrCompare::getDetailId,invoiceOcrDetailList.get(0).getId().toString()));
                if(CollectionUtils.isNotEmpty(approveOcrCompareList)){
                    loanOcrResultVO.setOcrType(invoiceOcrDetailList.get(0).getOcrType());
                    loanOcrResultVO.setResult(invoiceOcrDetailList.get(0).getOcrResult());
                    loanOcrResultVO.setApproveOcrCompareList(approveOcrCompareList);
                } else {
                    loanOcrResultVO.setOcrType(invoiceOcrDetailList.get(0).getOcrType());
                    loanOcrResultVO.setResult(invoiceOcrDetailList.get(0).getOcrResult());
                    loanOcrResultVO.setRemark(invoiceOcrDetailList.get(0).getRemark());
                }
                loanOcrResultVOList.add(loanOcrResultVO);
            }
        }
        //获取最近银行卡验证结果
        List<ApproveOcrDetail> bankOcrDetailList = approveOcrDetailService.list(Wrappers.<ApproveOcrDetail>query().lambda()
                .eq(ApproveOcrDetail::getContractNo,condition.getContractNo())
                .eq(ApproveOcrDetail::getOcrType,OcrTypeEnum.BANK_CARD.getValue())
                .orderByDesc(ApproveOcrDetail::getCreateTime));
        if(CollectionUtils.isNotEmpty(bankOcrDetailList)){
            if(bankOcrDetailList.get(0).getOcrResult()!=null && bankOcrDetailList.get(0).getOcrResult()!=""){
                LoanOcrResultVO loanOcrResultVO = new LoanOcrResultVO();
                List<ApproveOcrCompare> approveOcrCompareList = this.list(Wrappers.<ApproveOcrCompare>query().lambda()
                        .eq(ApproveOcrCompare::getDetailId,bankOcrDetailList.get(0).getId().toString()));
                if(CollectionUtils.isNotEmpty(approveOcrCompareList)){
                    loanOcrResultVO.setOcrType(bankOcrDetailList.get(0).getOcrType());
                    loanOcrResultVO.setResult(bankOcrDetailList.get(0).getOcrResult());
                    loanOcrResultVO.setApproveOcrCompareList(approveOcrCompareList);
                } else {
                    loanOcrResultVO.setOcrType(bankOcrDetailList.get(0).getOcrType());
                    loanOcrResultVO.setResult(bankOcrDetailList.get(0).getOcrResult());
                    loanOcrResultVO.setRemark(bankOcrDetailList.get(0).getRemark());
                }
                loanOcrResultVOList.add(loanOcrResultVO);
            }
        }
        //获取最近还款人身份证验证结果
        List<ApproveOcrDetail> idCardOcrDetailList = approveOcrDetailService.list(Wrappers.<ApproveOcrDetail>query().lambda()
                .eq(ApproveOcrDetail::getContractNo,condition.getContractNo())
                .eq(ApproveOcrDetail::getOcrType,OcrTypeEnum.ID_CARD.getValue())
                .orderByDesc(ApproveOcrDetail::getCreateTime));
        if(CollectionUtils.isNotEmpty(idCardOcrDetailList)){
            if(idCardOcrDetailList.get(0).getOcrResult()!=null && idCardOcrDetailList.get(0).getOcrResult()!=""){
                LoanOcrResultVO loanOcrResultVO = new LoanOcrResultVO();
                List<ApproveOcrCompare> approveOcrCompareList = this.list(Wrappers.<ApproveOcrCompare>query().lambda()
                        .eq(ApproveOcrCompare::getDetailId,idCardOcrDetailList.get(0).getId().toString()));
                if(CollectionUtils.isNotEmpty(approveOcrCompareList)){
                    loanOcrResultVO.setOcrType(idCardOcrDetailList.get(0).getOcrType());
                    loanOcrResultVO.setResult(idCardOcrDetailList.get(0).getOcrResult());
                    loanOcrResultVO.setApproveOcrCompareList(approveOcrCompareList);
                } else {
                    loanOcrResultVO.setOcrType(idCardOcrDetailList.get(0).getOcrType());
                    loanOcrResultVO.setResult(idCardOcrDetailList.get(0).getOcrResult());
                    loanOcrResultVO.setRemark(idCardOcrDetailList.get(0).getRemark());
                }
                loanOcrResultVOList.add(loanOcrResultVO);
            }
        }
        return loanOcrResultVOList;
    }


}
